package com.ljfiction.book.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ljfiction.book.fragment.ranking.RankingPareantBean;
import com.ljfiction.book.MyView.BorderTextView;
import com.ljfiction.book.MyView.RoundAngleImageView;
import com.ljfiction.book.R;
import com.ljfiction.book.tools.ScreenUtils;
import com.ljfiction.book.tools.StringTool;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2016/10/12.
 */

public class PtrListAdapter extends BaseAdapter {
    private Context context;
    private List<RankingPareantBean> datas = new ArrayList<>();
    private int posn=1;
    private String[] dataname={"金椒","小米椒","点击","新书","畅销","吐槽","更新","投喂辣椒"};


    public PtrListAdapter(List<RankingPareantBean> datas, Context context, String type) {
        this.datas = datas;
        this.context=context;
        posn=Integer.valueOf(type)-1;

    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView==null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.ranking_list_item, null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        switch (position){
            case 0:
                viewHolder.numerone.setImageResource(R.mipmap.gold);
                break;
            case 1:
                viewHolder.numerone.setImageResource(R.mipmap.silver);
                break;
            case 2:
                viewHolder.numerone.setImageResource(R.mipmap.copper);
                break;
            default:
                viewHolder.numerone.setVisibility(View.GONE);
                break;
        }
        viewHolder.rankingitemBookname.setText(datas.get(position).getBookName());
        viewHolder.rankingitemAuther.setText("作者："+datas.get(position).getWriterName());
        viewHolder.borderTextView.setText(StringTool.getClicknum(datas.get(position).getTempTicketSum())+dataname[posn]);
        viewHolder.rankingitemSynopsis.setText(datas.get(position).getBookIntroduction());
        Glide.with(context).load(datas.get(position).getBookImage()).override(ScreenUtils.getNum(context,90),ScreenUtils.getNum(context,120)).into(viewHolder.rankingitemImag);
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.rankingitem_imag)
        RoundAngleImageView rankingitemImag;
        @BindView(R.id.rankingitem_bookname)
        TextView rankingitemBookname;
        @BindView(R.id.rankingitem_auther)
        TextView rankingitemAuther;
        @BindView(R.id.rankingitem_synopsis)
        TextView rankingitemSynopsis;
        @BindView(R.id.jinpiaonum)
        BorderTextView borderTextView;
        @BindView(R.id.numerone)
        ImageView numerone;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
